package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryBrandService categoryBrandService;
    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        Brand brand = brandService.getById(id);
        BrandDTO brandDTO=new BrandDTO(brand);
        return ResponseEntity.ok(brandDTO);
    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        List<Brand> brands = brandService.listByIds(ids);
        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(brands);
        return ResponseEntity.ok(brandDTOS);
    }

    /**
     * 根据分类id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long id){
        List<BrandDTO> brandDTOS = brandService.queryBrandByCategoryId(id);
        return ResponseEntity.ok(brandDTOS);
    }

    /**
     * 分页查询品牌
     * @param key
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ){

        PageDTO<BrandDTO> brandDTOPageDTO = brandService.queryBrandByPage(key, page, rows);
        return ResponseEntity.ok(brandDTOPageDTO);
    }

    /**
     * 新曾品牌
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id){
        //删除中间表
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
        //删除品牌表
        brandService.removeById(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
}
